package com.mrx.commons.task.model;

import cn.hutool.cron.task.Task;
import com.mrx.commons.task.SchedulerTask;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Optional;
import java.util.Set;

/**
 * @author Mr.X
 * @since 2024-01-12 17:34
 **/
@Getter
@RequiredArgsConstructor(staticName = "of")
public class ScheduleTaskWrapper implements Task {

    private final XTask task;
    private String result;

    public String getTaskName() {
        return task.getVo().getName();
    }

    public boolean mailNotify() {
        return task.getVo().getMailNotify();
    }

    public String notifyMail() {
        return task.getVo().getNotifyMail();
    }

    public Set<TaskLifeCycle> notifyStatus() {
        return task.getVo().getNotifyStatus();
    }

    public Optional<String> getAndClearResult() {
        String temp = result;
        result = null;
        return Optional.ofNullable(temp);
    }

    @Override
    public void execute() {
        result = SchedulerTask.runTask(getTaskName());
    }

}
